package cn.ren.cloud.controller;

import cn.ren.cloud.dto.AlipayNotifyDto;
import cn.ren.cloud.dto.PayApplyDto;
import cn.ren.cloud.service.IPayOrderService;
import cn.ren.cloud.domain.PayOrder;
import cn.ren.cloud.query.PayOrderQuery;
import cn.ren.cloud.result.JsonResult;
import cn.ren.cloud.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author 23508
 */
@RestController
@RequestMapping("/payOrder")
public class PayOrderController {

    public final IPayOrderService payOrderService;

    public PayOrderController(IPayOrderService payOrderService) {
        this.payOrderService = payOrderService;
    }

    /**
     * 保存和修改公用的
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody PayOrder payOrder) {
        if (payOrder.getId() != null) {
            payOrderService.updateById(payOrder);
        } else {
            payOrderService.insert(payOrder);
        }
        return JsonResult.success();
    }

    /**
     * 删除对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id) {
        payOrderService.deleteById(id);
        return JsonResult.success();
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id") Long id) {
        return JsonResult.success(payOrderService.selectById(id));
    }


    /**
     * 查询所有对象
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JsonResult list() {
        return JsonResult.success(payOrderService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JsonResult page(@RequestBody PayOrderQuery query) {
        Page<PayOrder> page = new Page<PayOrder>(query.getPage(), query.getRows());
        page = payOrderService.selectPage(page);
        return JsonResult.success(new PageList<PayOrder>(page.getTotal(), page.getRecords()));
    }

    /**
     * checkPayOrder
     */

    @RequestMapping(value = "/checkPayOrder/{orderNo}", method = RequestMethod.GET)
    public JsonResult checkPayOrder(@PathVariable("orderNo") String orderNo) {
        PayOrder payOrder = payOrderService.selectByOrderNo(orderNo);
        return payOrder == null ? JsonResult.error() : JsonResult.success();
    }

    @RequestMapping(value = "/apply", method = RequestMethod.POST)
    public JsonResult apply(@RequestBody PayApplyDto payApplyDto) {
        String payResult = payOrderService.apply(payApplyDto);
        return JsonResult.success(payResult);
    }


    /**
     * 支付宝异步通知接口
     * @param alipayNotifyDto
     * @return
     */
    @RequestMapping(value = "/ali-callback",method = RequestMethod.POST)
    public String callback(AlipayNotifyDto alipayNotifyDto){
        return payOrderService.callback(alipayNotifyDto);
    }
}
